package hlhj.com.carviewpad.utils

import android.content.Context
import android.util.Log
import com.example.mymvp.log
import hlhj.com.carviewpad.javabean.AdvBean
import hlhj.com.carviewpad.presenters.MainPresenter
import java.io.File
import java.nio.file.Files.isDirectory
import java.util.*
import kotlin.collections.ArrayList


/**
 * Created by Never Fear   on 2019\1\15 0015.
Never More....
 */
object FileUtil {
    fun getFileName(fileAbsolutePath: String,c: Context): ArrayList<AdvBean>? {
        try {
            val advs=ArrayList<AdvBean>()
            val file = File(fileAbsolutePath)
            val subFile = file.listFiles()
            for (iFileLength in subFile.indices) {
                // 判断是否为文件夹
                if (!subFile[iFileLength].isDirectory()) {
                    try {
                        val filename = subFile[iFileLength].getName()
                        Log.e("eee", "文件名 ： " + filename)
                        val advBean = AdvBean()
                        val split = filename.split("_")
                        log("fhpp","${filename}组装时id=${split[1]} 名字=${split[2]} 类型=${split[3]}")
                        advBean.id=split[1]
                        advBean.aName=split[2]
                        advBean.type=split[3]
                        advBean.sTime=split[4]
                        advBean.eTime=split[5]
                        advBean.aName=split[6]
                        advBean.aId=split[7]
                        advBean.duration=split[8]
                        advBean.filePath=subFile[iFileLength].absolutePath
                        if (advBean.aName?.equals(SPHelper.getLocation(c))!!){
                            //匹配区域
                            advs.add(advBean)
                        }
                    }catch (e:Exception){
                        e.printStackTrace()
                    }
                }
            }
            return advs
        }catch (e:Exception){
            return null
            log("fhp","报错了${e}")
        }

    }

    fun getCovers(fileAbsolutePath: String): ArrayList<String>? {
        try {
            val paths=ArrayList<String>()
            val file = File(fileAbsolutePath)
            val subFile = file.listFiles()

            for (iFileLength in subFile.indices) {
                // 判断是否为文件夹
                if (!subFile[iFileLength].isDirectory()) {
                    val filePath = subFile[iFileLength].absolutePath
                    paths.add(filePath)
                }
            }
            return paths
        }catch (e:Exception){
            return null
            log("fhp","文件报错了${e}")
        }

    }
}